package com.example.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.example.entity.UserchecksEntity;
import com.example.entity.UserchecksVoEntity;
import com.example.service.UserchecksService;

@RequestMapping("/userchecks")
@RestController
public class UserchecksController {
	@Autowired
	private UserchecksService userchecksService;
	
	/**
	 * 多条件查询
	 * @param userVO
	 * @return
	 */
	@RequestMapping("/GetUsersLayUI")
	public Map<String, Object> GetUsersLayUI(UserchecksVoEntity userchecksVoEntity){
		Map<String, Object> reMap=new HashMap<String, Object>();
		userchecksVoEntity.setOffset((userchecksVoEntity.getPage()-1)*userchecksVoEntity.getLimit());
		List<UserchecksEntity> list=userchecksService.GetUsersLayUI(userchecksVoEntity);
		int count=userchecksService.getCount(userchecksVoEntity);
		reMap.put("code","0");
		reMap.put("msg", "success");
		reMap.put("count", count);
		reMap.put("data", list);
		return reMap;
	}
	
	/**
	 *  查询全部
	 * @param id
	 * @return
	 */
	@PostMapping(path = "/selectListAll")
	public Map<String, Object> selectListAll() { // 受影响的行数
		
		List<UserchecksEntity> list = userchecksService.selectListAll();
		Map<String, Object> rsMap =new HashMap<String, Object>();
		rsMap.put("code", "code");
		rsMap.put("msg","");
		rsMap.put("count", list.size());
		rsMap.put("data",list);
		return rsMap;

	}
	/**
	 * 签到
	 * @param userchecksEntity
	 * @return
	 */
	@PostMapping(path = "/signIn")
	public Map<String,Object> signIn(UserchecksEntity userchecksEntity){
		//受影响的行数
		int rows = userchecksService.singIn(userchecksEntity);
		Map<String, Object> rsMap = new HashMap<String, Object>();
		rsMap.put("code", rows > 0 ? "0" : "1");
		rsMap.put("msg", rows > 0 ? "success" : "");
		return rsMap;
	}
	
	/**
	 * 签退
	 * @param userchecksEntity
	 * @return
	 */
	@PostMapping(path = "/signOut")
	public Map<String, Object> signOut(UserchecksEntity userchecksEntity) {
		Map<String, Object> rsMap = new HashMap<String, Object>();
		UserchecksEntity Check= userchecksService.selectSigneInBefo10(userchecksEntity.getUserId());
		if (Check == null ) {
			int rows= userchecksService.singOut(userchecksEntity);
			rsMap.put("code", rows > 0 ? "0" : "1");
			rsMap.put("msg", rows > 0 ? "success" : "");
		}else if (Check !=null) {
			rsMap.put("msg","time");
		}
		return rsMap;

	}
	
	/**
	 * 查询当前用户是否签到
	 * @param userid
	 * @return
	 */
	@PostMapping("/selectIsSignIN")
	public Map<String, Object> selectIsSignIN(int userId) {
		// 受影响的行数
		UserchecksEntity rows = userchecksService.selectisSignIn(userId);
		Map<String, Object> rsMap = new HashMap<String, Object>();
		if (rows == null) {
			rsMap.put("code", 0);
		} else if (rows != null) {
			rsMap.put("code", rows.getCheckState());
		}
		return rsMap;
}



}
